$(document).ready(function () {
  var table = $('table#table').DataTable({
    ajax: {
      url: baseUrl + '/datatable',
      data : function(d) {
		var no    = $('.search-form').find('input[name="documentNumber"]').val();
        var type = $('.search-form').find('select#type').val();
        var status = $('.search-form').find('select#status').val();
        var material = $('.search-form').find('select#material').val();
        d.no = no;
        d.type = type;
        d.status = status;
        d.material = material;
		return d;
      }
    },
    order: [
      [9, 'desc']
    ],
    columns: [{
      data: 'id',
      sortable: false,
      defaultContent: '',
      'class': 'text-center',
      render: function (data, type, row, meta) {
        return '<div class="checkbox"><input type="checkbox" name="ids" value="' + data + '"/><label for="ids"/></div>'
      }

     },{
      name: 'documentNumber',
      data: 'documentNumber'
    },{
      name: 'type',
      data: 'type',
      render: function (data, type, row, meta) {
      	return VALUES_MAP_DICT_TYPE && VALUES_MAP_DICT_TYPE[data] ? VALUES_MAP_DICT_TYPE[data].text : '';
      }
    },{
      name: 'refSoNumber',
      data: 'refSoNumber'
    },{
      name: 'refAsnNumber',
      data: 'refAsnNumber'
    },{
      name: 'paymentTerms',
      data: 'paymentTerms'
    },{
      name: 'consigneeName',
      data: 'consigneeName'
    },{
      name: 'planLoadPlace',
      data: 'planLoadPlace'
    },{
      name: 'planLoadDoor',
      data: 'planLoadDoor'
    },{
      name: 'deliveryFromPlace',
      data: 'deliveryFromPlace'
    },{
      name: 'waveId',
      data: 'waveId',
      render: function (data, type, row, meta) {
          if ((data==undefined) || (data==null)||(data=='')) {return 'N'}
          return 'Y';
      }
    },{
      data: "id",
      defaultContent: '',
      sortable: false,
      'class': 'text-center',
      render: function (data, type, row, meta) {
         return '<a class="btn btn-default btn-xs" href="'+rootUrl+'/outbound/posodetails/'+ row.id + '"><i class="fa fa-list"></i></a>';
      }
    },{
      name: 'createSource',
      data: 'createSource'
    },{
      name: 'createdDate',
      data: 'createdDate'
    },{
      name: 'status',
      data: 'status',
      render: function (data, type, row, meta) {
            if (data == '0000')  return '创建单据';
            if (data == '0100')  return '生成波次';
            if (data == '0300')  return '完全分配';
            if (data == '0250')  return '部分分配';
            if (data == '0650')  return '部分拣选';
            if (data == '0700')  return '出库完成';
            if (data == '0850')  return '单据作废';
            if (data == '0880')  return '关闭';
            return data;
        }
    },{
        name: 'notes',
        data: 'notes'
    },{ 
      data: "enabled",
      defaultContent: '',
      sortable: false,
      'class': 'text-center',
      render: function (data, type, row, meta) {
        var result = '<a class="btn btn-xs btn-default enable" href="javascript:;">';
        if (data) {
          result += '<i title="激活/禁用" class="fa fa-check-square-o"/>';
        } else {
          result += '<i title="激活/禁用" class="fa fa-square-o"/>';
        }
        result += '</a>';
        return result;
      }
    }, {
      data: "id",
      defaultContent: '',
      sortable: false,
      'class': 'text-center',
      render: function (data, type, row, meta) {
        return ('<div class="btn-group">' +
          '<a  class="btn btn-default btn-xs" title="修改" href="' + baseUrl + '/edit/' + row.id + '"><i class="fa fa-edit"></i></a>' +
          '<a class="btn btn-default btn-xs del" title="删除" href="#"><i class="fa fa-trash-o"></i></a>' +
          '</div>' +
          '&nbsp;&nbsp;');
      }
    }]
  });

  table.on('click', '.del', function () {
    var id = table.row($(this).parents('tr')).data().id;
    $("#delForm #ids").val(id);
    if (id) {
      bootbox.confirm("确定要删除数据吗?", function (result) {
        if (result) {
          $("#delForm").submit();
        }
      });
    }
  });

  $(".btn.alcAll").on("click", function (e) {
    var ids = [];
    $('#table>tbody input:checked').each(function (i, item) {
      ids.push($(item).val());
    });
    $("#alcForm #ids").val(ids.join(","));
    if (ids.length > 0) {
      bootbox.confirm("确定要分配选中的数据吗?", function (result) {
        if (result) {
          $("#alcForm").submit();
        }
      });
    } else {
      bootbox.alert("请选择要分配的数据.");
    }
  });

    $(".btn.createWaveAll").on("click", function (e) {
        var ids = [];
        $('#table>tbody input:checked').each(function (i, item) {
            ids.push($(item).val());
        });
        $("#createWaveForm #ids").val(ids.join(","));
        if (ids.length > 0) {
            bootbox.confirm("确定要将订单合并生成波次吗?", function (result) {
                if (result) {
                    $("#createWaveForm").submit();
                }
            });
        } else {
            bootbox.alert("请选择要合并波次的数据.");
        }
    });

    $(".btn.addToWaveAll").on("click", function (e) {
        var ids = [];
        $('#table>tbody input:checked').each(function (i, item) {
            ids.push($(item).val());
        });
        $("#addToWaveForm #ids").val(ids.join(","));
        if (ids.length > 0) {
            bootbox.confirm("确定要将订单生成波次吗?", function (result) {
                if (result) {
                    $("#addToWaveForm").submit();
                }
            });
        } else {
            bootbox.alert("请选择要生成波次的数据.");
        }
    });



    /**
  $(".btn.printAll").on("click", function (e) {
      var ids = [];
      $('#table>tbody input:checked').each(function (i, item) {
          ids.push($(item).val());
      });
      $("#printForm #ids").val(ids.join(","));
      if (ids.length > 0) {
          bootbox.confirm("确定要打印的选定的数据吗?", function (result) {
              if (result) {
                  $("#printForm").submit();
              }
          });
      } else {
          bootbox.alert("请选择要打印的数据.");
      }
  });
**/

  $(".btn.closeAll").on("click", function (e) {
    var ids = [];
    $('#table>tbody input:checked').each(function (i, item) {
      ids.push($(item).val());
    });
    $("#closeForm #ids").val(ids.join(","));
    if (ids.length > 0) {
      bootbox.confirm("确定要关闭的数据吗?", function (result) {
        if (result) {
          $("#closeForm").submit();
        }
      });
    } else {
      bootbox.alert("请选择要关闭的数据.");
    }
  });

    $(".btn.delAll").on("click", function (e) {
        var ids = [];
        $('#table>tbody input:checked').each(function (i, item) {
            ids.push($(item).val());
        });
        $("#delForm #ids").val(ids.join(","));
        if (ids.length > 0) {
            bootbox.confirm("确定要删除选中的数据吗?", function (result) {
                if (result) {
                    $("#delForm").submit();
                }
            });
        } else {
            bootbox.alert("请选择要删除的数据.");
        }
    });

  table.on('click', '.enable', function () {
    var that = this;
    var data = table.row($(this).parents('tr')).data();
    var id = data.id;
    var enabled = data.enabled;
    bootbox.confirm("确定要更新选中的数据吗?", function(resul) {
	if (resul) {
	    $.get(baseUrl + "/enable", {
	      enabled: !enabled,
	      ids: id
	    }, function (res) {
	      if (res) {
	        data.enabled = !enabled;
	        $(that).find("i").removeClass("fa-" + (enabled ? "check-" : "") + "square-o").addClass("fa-" + (!enabled ? "check-" : "") + "square-o");
	        toastr.info('操作成功!');
	      }
	
	    });
	}});   
  });

  $('.search-form').on("submit", function (e) {
    table.draw();
    //阻止表单submit
    return false;
  });

});
